﻿Imports System
Imports System.Text
Imports System.Collections.Generic
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports Formition

<TestClass()> Public Class UnitTest1

    Public Function RandomGeneratedInteger(ByVal FromInt As Integer, ByVal ToInt As Integer) As Integer


        Dim TempChar
        Dim outChar As Integer = FromInt - 1

        Do Until outChar >= FromInt

            TempChar = System.Math.Round((Rnd(Len(ToInt.ToString)) * ToInt), 0) + FromInt


            'valid number
            If TempChar >= FromInt And TempChar <= ToInt Then
                outChar = TempChar
            Else
                'junk character
                outChar = FromInt - 1
            End If

        Loop

        Return outChar


    End Function


    Public Function RandomGeneratedString(ByVal LengthMin As Integer, ByVal LengthMax As Integer) As String

        Dim NewString As String = ""

        Dim x As Integer
        Dim Y As Integer = Me.RandomGeneratedInteger(LengthMin, LengthMax)
        For x = 0 To Y
            NewString = NewString & Me.RandomChar
        Next

        Return NewString

    End Function


    Function RandomChar() As String
        Dim TempChar
        Dim outChar As Integer = 0

        Do Until outChar > 0

            TempChar = System.Math.Round((Rnd() * (Second(Now) + 48)), 0) + 48

            'valid number
            If TempChar >= 0 And TempChar <= 5000 Then
                outChar = TempChar
            Else
                'junk character
                outChar = 0
            End If

        Loop

        RandomChar = Chr(outChar)

    End Function


    Private testContextInstance As TestContext

    '''<summary>
    '''Gets or sets the test context which provides
    '''information about and functionality for the current test run.
    '''</summary>
    Public Property TestContext() As TestContext
        Get
            Return testContextInstance
        End Get
        Set(ByVal value As TestContext)
            testContextInstance = value
        End Set
    End Property

#Region "Additional test attributes"
    '
    ' You can use the following additional attributes as you write your tests:
    '
    ' Use ClassInitialize to run code before running the first test in the class
    ' <ClassInitialize()> Public Shared Sub MyClassInitialize(ByVal testContext As TestContext)
    ' End Sub
    '
    ' Use ClassCleanup to run code after all tests in a class have run
    ' <ClassCleanup()> Public Shared Sub MyClassCleanup()
    ' End Sub
    '
    ' Use TestInitialize to run code before running each test
    ' <TestInitialize()> Public Sub MyTestInitialize()
    ' End Sub
    '
    ' Use TestCleanup to run code after each test has run
    ' <TestCleanup()> Public Sub MyTestCleanup()
    ' End Sub
    '
#End Region

    <TestMethod()> Public Sub TestMethod1()

        Exit Sub

        Dim SuccessfulRegistrars As Integer = 0
        Dim SuccessfulUsers As Integer = 0
        Dim FailedPasswords As Integer = 0

        For x = 0 To 10

            'test logging in functionality
            Common.Userid = Nothing
            Dim Username As String = RandomGeneratedString(1, 200)
            Dim Password As String = RandomGeneratedString(1, 200)
            If Common.RegisterUser(Username, Password, "") Then
                SuccessfulRegistrars = SuccessfulRegistrars + 1
                If Login(Username, Password) Then
                    SuccessfulUsers = SuccessfulUsers + 1
                Else
                    FailedPasswords = FailedPasswords + 1
                End If
            Else
                Call MsgBox("Registration Failed")
            End If

            If Common.Userid <> Nothing Then
                Call Common.DeleteTestUser(Common.Userid)
            End If

            'test create password functionality
            'Common.PasswordEdit(Common.SkipField, Common.Userid, RandomGeneratedString(3, 100), RandomGeneratedString(0, 2000), RandomGeneratedString(0, 2000), RandomGeneratedString(0, 100), RandomGeneratedString(0, 100), Common.GetDate, Common.CurrentlySelectedUserCategory, "", Common.GetDate)
        Next

        If SuccessfulUsers <> SuccessfulRegistrars Then
            Call MsgBox("Failed!!! , Number of New Users did not match number of logins" & SuccessfulUsers.ToString & "/" & SuccessfulRegistrars.ToString)
        Else
            Call MsgBox("Success, total of " & SuccessfulUsers.ToString & " created")
        End If

    End Sub



    <TestMethod()> Public Sub TestEncryption()

        Dim SuccessfulRegistrars As Integer = 0
        Dim SuccessfulUsers As Integer = 0
        Dim FailedPasswords As Integer = 0
        Dim x As Integer

        For x = 0 To 1000009

            'test logging in functionality

            Dim Key As String = Left(RandomGeneratedString(1, 200) & "kasjdfkjsd;lfjaslkfjaslf;jdsl;fjasdl;fjasdlfjasdl;fjasdlfjasldkjflsdakj", 100)
            Dim String1 As String = RandomGeneratedString(1, 200)
            Dim TestString As String = Encrypt(Key, String1)

            SuccessfulRegistrars = SuccessfulRegistrars + 1

            If Decrypt(Key, TestString) = String1 Then
                SuccessfulUsers = SuccessfulUsers + 1
            Else
                Call MsgBox(Key & "-" & String1)
                FailedPasswords = FailedPasswords + 1
            End If


            'test create password functionality
            'Common.PasswordEdit(Common.SkipField, Common.Userid, RandomGeneratedString(3, 100), RandomGeneratedString(0, 2000), RandomGeneratedString(0, 2000), RandomGeneratedString(0, 100), RandomGeneratedString(0, 100), Common.GetDate, Common.CurrentlySelectedUserCategory, "", Common.GetDate)
        Next

        If SuccessfulUsers <> SuccessfulRegistrars Then
            Call MsgBox("Failed!!! , Number of New Users did not match number of logins" & SuccessfulUsers.ToString & "/" & SuccessfulRegistrars.ToString)
        Else
            Call MsgBox("Success, total of " & SuccessfulUsers.ToString & " created")
        End If

    End Sub
End Class
